#import "NuGraphicCell.h"
#import <appkit/NXHelpPanel.h>
#import <appkit/Application.h>
#import <appkit/workspaceRequest.h>
#import <appkit/NXBitmapImageRep.h>
#import <appkit/View.h>
#import <objc/List.h>
#import <strings.h>
#import "WorkspaceManager.h"
#import <libc.h>
#import <appkit/tiff.h>
extern id Nu ; /* for debugging */
@implementation NuGraphicCell: NXGraphicCell
{
}


- (BOOL) trackMouse:(NXEvent *) theEvent inRect:(const NXRect *) cellFrame ofView:controlView ;
{ // inspired by some old code from Joe Freeman of NeXT inc's
  // public domain multimedia objects
  if(theEvent->data.mouse.click == 2) // if a double-click...
  { id controlWindow ;
    char *path, *fullPath ;
    int fullPathLength ;
    controlWindow = [controlView window] ;
    // my filename is in the contents ivar...if we are
    // in a helpPanel, message it to create a full pathname
    if([controlWindow isMemberOf: [NXHelpPanel class]])
    { // we are in a help panel...do the following contortions
      // in order to get our full pathname
      char *helpFile ;
      path = (char *) [controlWindow helpDirectory] ;
      helpFile = [controlWindow helpFile] ;
      fullPathLength = strlen(path) + strlen(helpFile) + strlen(contents) ;
      fullPath = (char *) alloca(fullPathLength + 3) ;
      sprintf(fullPath,"%s/%s/%s",path,helpFile,contents) ;
      [[Application workspace] openFile:fullPath] ;
     }
     else if([controlWindow isKindOf: [WorkspaceManager class]])
     { // we are in a workspace (or one of its subclasses) window.
       // Easier to get full path, but if workspace has not been
       // saved, it is impossible.
       path = [controlWindow fileName] ;
       if(path[0] == '\0') // window not saved...can't open it
       { NXRunAlertPanel("Nu",
          "Can't do this operation from an untitled document",
           NULL,NULL,NULL) ;
         return NO ; 
       }
       fullPathLength = strlen(path) + strlen(contents) ;
       fullPath = (char *) alloca(fullPathLength + 2) ;
       sprintf(fullPath,"%s/%s",path,contents) ;
       [[Application workspace] openFile:fullPath] ;
     }
     // else do nothing
  }
  return YES;
}


char * contentsOfStream(NXStream *aStream)
{ // push a null character (assures that if
  // aStream contains text, it is a NULL terminated
  // cstring) onto aStream, return a pointer to aStream's
  // memorybuffer
 int len, maxLen ;
 char *buffer ;
 NXPutc(aStream,'\0') ;
 NXGetMemoryBuffer(aStream,&buffer,&len,&maxLen) ;
 return buffer ;
}   

- delayedSave: controlWin ;
{ // Write our image to a file, using controlWin
  // to get the path, and contents to provice the
  // the file name.
  // First, check that our window understands fileName...
  if([controlWin respondsTo: @selector(fileName)])
  { NXStream *nameStream, *imageStream ;
    nameStream = NXOpenMemory(NULL,0,NX_WRITEONLY);
    NXPrintf(nameStream,"%s/%s",[controlWin fileName],contents) ;
    imageStream = NXOpenMemory(NULL,0,NX_WRITEONLY);
    [[support bestRepresentation]writeTIFF:imageStream  
       usingCompression:NX_TIFF_COMPRESSION_LZW];
  // [support writeTIFF:stream usingCompression:NX_TIFF_COMPRESSION_NONE];
    NXSaveToFile(imageStream, contentsOfStream(nameStream)) ;
    NXCloseMemory(nameStream, NX_FREEBUFFER);
    NXCloseMemory(imageStream, NX_FREEBUFFER);
  }
  return self ;
}


- writeRichText:(NXStream *)stream forView:controlView ;
{ // redefine this method so that cells which have
  // been created via NuText's pasteScreen: method
  // save their image to disk
  if(!strncmp(contents,NUSCREENPASTE,strlen(NUSCREENPASTE)))
  { id controlWin ;
    controlWin = [controlView window] ;
    // can't actually save yet because the rtfd directory
    // hasn't been created yet...schedule it for later...
    [self perform:@selector(delayedSave:) 
	with:controlWin  afterDelay:5 cancelPrevious:YES];
  }
 return [super writeRichText: stream forView: controlView] ;
}

@end
